package com.lh.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lh.entity.table.*;
import com.lh.mapper.*;
import com.lh.service.StatisticsService;
import com.lh.utils.Answer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Resource
    private ReminderInfoMapper reminderInfoMapper;

    @Resource
    private DeadlineNodeMapper deadlineNodeMapper;

    @Resource
    private ProblemSummaryInfoMapper problemSummaryInfoMapper;

    @Resource
    private RectificationInfoMapper rectificationInfoMapper;

    @Resource
    private TicketProblemInfoMapper ticketProblemInfoMapper;

    @Resource
    private TaskInfoMapper taskInfoMapper;

    @Resource
    private Task1InfoMapper task1InfoMapper;

    @Resource
    private Task2InfoMapper task2InfoMapper;

    @Override
    public Answer<Object> queryProjectCategory() {
        try{
            LambdaQueryWrapper<ProblemSummaryInfo> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
            objectLambdaQueryWrapper.select(ProblemSummaryInfo::getProjectCategory);
            objectLambdaQueryWrapper.isNotNull(ProblemSummaryInfo::getProjectCategory);
            List<ProblemSummaryInfo> problemSummaryInfos = problemSummaryInfoMapper.selectList(objectLambdaQueryWrapper);
            List<String> projectCategoryList = problemSummaryInfos.stream().map(ProblemSummaryInfo::getProjectCategory).distinct().filter(StrUtil::isNotBlank).collect(Collectors.toList());
            return Answer.ok(projectCategoryList);
        } catch (Exception e) {
            return Answer.error(e.getMessage());
        }
    }

    @Override
    public Answer<Object> queryAllData(JSONObject reqJson) {
        try{
            //DateTime date = DateUtil.date();
            JSONObject resJson = new JSONObject();
            JSONObject warnCountJson = new JSONObject();
            //所有
            int allCount = 0;
            //已完成
            int doneCount = 0;
            //已完成-延长
            int doneCountExtended = 0;
            //已完成-正常
            int doneCountNormal = 0;
            //未完成
            int incompleteCount = 0;
//            //未完成-延长
//            int incompleteExtended = 0;
//            //未完成-正常
//            int incompleteNormal = 0;
            //预警等级绿
            int warnLevel1 = 0;
            //预警等级黄
            int warnLevel2 = 0;
            //预警等级红
            int warnLevel3 = 0;

            String projectCategory = reqJson.getString("projectCategory");
            String areaName = reqJson.getString("areaName");
            String state = reqJson.getString("state");

            List<RectificationInfo> rectificationInfoList = CollectionUtil.newArrayList();
            List<TicketProblemInfo> ticketProblemInfoList = CollectionUtil.newArrayList();
            LambdaQueryWrapper<DeadlineNode> deadlineNodeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            LambdaQueryWrapper<ProblemSummaryInfo> problemSummaryInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            LambdaQueryWrapper<TicketProblemInfo> ticketProblemInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            if (StrUtil.isBlank(areaName)) {
                if (StrUtil.isNotBlank(projectCategory)) {
                    problemSummaryInfoLambdaQueryWrapper.eq(ProblemSummaryInfo::getProjectCategory, projectCategory);
                }
                List<ProblemSummaryInfo> problemSummaryInfos = problemSummaryInfoMapper.selectList(problemSummaryInfoLambdaQueryWrapper);
                for (ProblemSummaryInfo problemSummaryInfo : problemSummaryInfos) {
                    List<RectificationInfo> rectificationInfos = rectificationInfoMapper.selectList(new LambdaQueryWrapper<RectificationInfo>().eq(RectificationInfo::getSummaryId, problemSummaryInfo.getId()));
                    for (RectificationInfo rectificationInfo : rectificationInfos) {
                        Integer id = rectificationInfo.getId();
                        if(StrUtil.isNotBlank(state)){
                            deadlineNodeLambdaQueryWrapper.eq(DeadlineNode::getState, state);
                        }
                        deadlineNodeLambdaQueryWrapper.eq(DeadlineNode::getMainId, id);
                        deadlineNodeLambdaQueryWrapper.eq(DeadlineNode::getTypeName, "整改");
                        deadlineNodeLambdaQueryWrapper.orderByAsc(DeadlineNode::getDeadline);
                        List<DeadlineNode> deadlineNodes = deadlineNodeMapper.selectList(deadlineNodeLambdaQueryWrapper);
                        if (CollectionUtil.isNotEmpty(deadlineNodes)) {
                            DeadlineNode deadlineNode = deadlineNodes.get(0);
                            String deadlineState = deadlineNode.getState();
                            Date deadline = deadlineNode.getDeadline();
                            String warnLevel = deadlineNode.getWarnLevel();
                            if ("1".equals(deadlineState)) {
                                doneCount++;
                                if ("3".equals(warnLevel)) {
                                    doneCountExtended++;
                                } else {
                                    doneCountNormal++;
                                }
                            } else {
                                incompleteCount++;
                            }
                            if ("1".equals(warnLevel)) {
                                warnLevel1++;
                            } else if ("2".equals(warnLevel)) {
                                warnLevel2++;
                            } else if ("3".equals(warnLevel)) {
                                warnLevel3++;
                            }
                            rectificationInfoList.add(rectificationInfo);
                        }
                    }
                }

                allCount = doneCount + incompleteCount;
                warnCountJson.put("allCount", allCount);
                warnCountJson.put("doneCount", doneCount);
                warnCountJson.put("doneCountExtended", doneCountExtended);
                warnCountJson.put("doneCountNormal", doneCountNormal);
                warnCountJson.put("incompleteCount", incompleteCount);
                warnCountJson.put("warnLevel1", warnLevel1);
                warnCountJson.put("warnLevel2", warnLevel2);
                warnCountJson.put("warnLevel3", warnLevel3);
                resJson.put("warnCountJson", warnCountJson);
                resJson.put("rectificationInfoList", rectificationInfoList);
            }else{
                ticketProblemInfoLambdaQueryWrapper.eq(TicketProblemInfo::getAreaName, areaName);
                List<TicketProblemInfo> ticketProblemInfos = ticketProblemInfoMapper.selectList(ticketProblemInfoLambdaQueryWrapper);
                if (CollectionUtil.isNotEmpty(ticketProblemInfos)){
                    for (TicketProblemInfo ticketProblemInfo : ticketProblemInfos) {
                        Integer id = ticketProblemInfo.getId();
                        if(StrUtil.isNotBlank(state)){
                            deadlineNodeLambdaQueryWrapper.eq(DeadlineNode::getState, state);
                        }
                        deadlineNodeLambdaQueryWrapper.eq(DeadlineNode::getMainId, id);
                        deadlineNodeLambdaQueryWrapper.eq(DeadlineNode::getTypeName, "下发");
                        deadlineNodeLambdaQueryWrapper.orderByAsc(DeadlineNode::getDeadline);
                        List<DeadlineNode> deadlineNodes = deadlineNodeMapper.selectList(deadlineNodeLambdaQueryWrapper);
                        if (CollectionUtil.isNotEmpty(deadlineNodes)) {
                            DeadlineNode deadlineNode = deadlineNodes.get(0);
                            String deadlineState = deadlineNode.getState();
                            Date deadline = deadlineNode.getDeadline();
                            String warnLevel = deadlineNode.getWarnLevel();
                            if ("1".equals(deadlineState)) {
                                doneCount++;
                                if ("3".equals(warnLevel)) {
                                    doneCountExtended++;
                                } else {
                                    doneCountNormal++;
                                }
                            } else {
                                incompleteCount++;
                            }
                            if ("1".equals(warnLevel)) {
                                warnLevel1++;
                            } else if ("2".equals(warnLevel)) {
                                warnLevel2++;
                            } else if ("3".equals(warnLevel)) {
                                warnLevel3++;
                            }
                            ticketProblemInfoList.add(ticketProblemInfo);
                        }
                    }
                }

                allCount = doneCount + incompleteCount;
                warnCountJson.put("allCount", allCount);
                warnCountJson.put("doneCount", doneCount);
                warnCountJson.put("doneCountExtended", doneCountExtended);
                warnCountJson.put("doneCountNormal", doneCountNormal);
                warnCountJson.put("incompleteCount", incompleteCount);
                warnCountJson.put("warnLevel1", warnLevel1);
                warnCountJson.put("warnLevel2", warnLevel2);
                warnCountJson.put("warnLevel3", warnLevel3);
                resJson.put("warnCountJson", warnCountJson);
                resJson.put("ticketProblemInfoList", ticketProblemInfoList);
            }

            return Answer.ok(resJson);
        } catch (Exception e) {
            e.printStackTrace();
            return Answer.error(e.getMessage());
        }
    }


//    public List<ReminderInfo> getReminderInfo(List<ProblemSummaryInfo> problemSummaryInfos) {
//        for (ProblemSummaryInfo problemSummaryInfo : problemSummaryInfos) {
//            Integer id = problemSummaryInfo.getId();
//            List<TaskInfo> taskInfos = taskInfoMapper.selectList(new LambdaQueryWrapper<TaskInfo>().eq(TaskInfo::getSummaryId, id));
//            for (TaskInfo taskInfo : taskInfos) {
//                Integer taskId = taskInfo.getId();
//                List<Task1Info> task1Infos = task1InfoMapper.selectList(new LambdaQueryWrapper<Task1Info>().eq(Task1Info::getTaskId, taskId));
//                for (Task1Info task1Info : task1Infos) {
//                    Integer task1Id = task1Info.getId();
//                    List<Task2Info> task2Infos = task2InfoMapper.selectList(new LambdaQueryWrapper<Task2Info>().eq(Task2Info::getTask1Id, task1Id));
//
//                }
//            }
//        }
//    }

}
